<?php
	namespace Admin\Controller;
	use Admin\Model\AuthGroupModel;
	use Admin\Model\AuthRuleModel;
	class AuthManagerController extends AdminController{
		public function index(){
			$list=$this->lists('AuthGroup',array('module'=>'admin'),'id asc');
			$list=int_to_string($list);
			$this->assign('_list',$list);
			$this->meta_title='权限管理';
			$this->display();
		}
		/**
		 * 编辑管理员用户组
		 */
		 public function editgroup(){
			 $this->meta_title='编辑用户组';
			 //$auth_group=M('AuthGroup')->where($map)->find();
			 $auth_group = M('AuthGroup')->where( array('module'=>'admin','type'=>AuthGroupModel::TYPE_ADMIN,) )
                                    ->find(I('get.id',0));
			 $this->assign('auth_group',$auth_group);
			 $this->display('editgroup');
		 }
		 /**
		 * 创建管理员用户组
		 */
		 public function createGroup(){
			$this->meta_title='新增用户组';
			$this->display('editgroup');
		 }
		 /**
		 * 管理员用户组数据写入/更新
		 */
		 public function writeGroup(){
			if(isset($_POST['rules'])){
				 sort($_POST['rules']);
				 $_POST['rules']=implode(',',array_unique($_POST['rules']));
				 //var_dump($_POST['rules']);

			 }
			$_POST['module'] =  'admin';
			$_POST['type']   =  AuthGroupModel::TYPE_ADMIN;
			$AuthGroup       =  D('AuthGroup');
			
				 $data=$AuthGroup->create();
				 if($data){
					 if (empty($data['id']) ) {
						$r = $AuthGroup->add();
					 }
					 else{
						$r = $AuthGroup->save();
					}
					if($r===false){
						$this->error('操作失败'.$AuthGroup->getError());
					}
					else{
						 $this->success('操作成功!',U('index'));
					}
				 }
				 else{
					 $this->error('操作失败'.$AuthGroup->getError());
				 }
		 }

		 /**
		 * 访问授权页面
		 */
		public function access(){
			$auth_group=M('AuthGroup')->where(array('status'=>array('egt',0),'module'=>'admin','type'=>AuthGroupModel::TYPE_ADMIN))->getfield('id,title,rules');
			//var_dump($auth_group);
			$node_list=$this->returnNodes();
			//echo "<pre/>";print_r($node_list);
			$map= array('module'=>'admin','type'=>AuthRuleModel::RULE_MAIN,'status'=>1);
			$main_rules=M('AuthRule')->where($map)->getField('name,id');
			//var_dump($main_rules);
		    $map         = array('module'=>'admin','type'=>AuthRuleModel::RULE_URL,'status'=>1);
            $child_rules = M('AuthRule')->where($map)->getField('name,id');
			//var_dump($child_rules);
			$this->assign('auth_group',$auth_group);
			$this->assign('node_list',$node_list);
			$this->assign('main_rules',$main_rules);
			$this->assign('auth_rules',$child_rules);
			$this->assign('this_group', $auth_group[(int)I('get.group_id')]);
			$this->meta_title = '访问授权';
			$this->display('managergroup');
		}
		/**
		 * 状态修改
		 */
		public function changeStatus($method=null){
			if ( empty($_REQUEST['id']) ) {
				$this->error('请选择要操作的数据!');
			}
			switch ( strtolower($method) ){
				case 'forbidgroup':
					$this->forbid('AuthGroup');
					break;
				case 'resumegroup':
					$this->resume('AuthGroup');
					break;
				case 'deletegroup':
					$this->delete('AuthGroup');
					break;
				default:
					$this->error($method.'参数非法');
			}
		}
		/**
		 * 将分类添加到用户组的编辑页面
		 */
		 public function category(){

		 }

	}
?>